//
//  FinishTaskViewController.m
//  BaishidaiKnight
//
//  Created by mac book on 2019/1/28.
//  Copyright © 2019年 sy. All rights reserved.
//

#import "FinishTaskViewController.h"
#import "FinishTaskTableViewCell.h"

#import "FinishTaskDetailViewController.h"

#define SEGHEIGHT 40/HEIGHT_6S_SCALE
#define Cell_H  60.f/HEIGHT_6S_SCALE


@interface FinishTaskViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong) BSDPager* page;
@property(nonatomic,strong) UILabel* titleLabel;

@end

@implementation FinishTaskViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"已完成的任务";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    [self creatSubViews];

    
}


-(void)creatSubViews{
    
    
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.tableView];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(SEGHEIGHT);
        
    }];
    
    
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view);

    }];
    
    [self refereshDatasource];
    
}

#pragma mark - Data source
-(void)getData{
    
    
    NSDictionary * arg = @{
                           @"ince":@"get_overorder_list",
                           @"emp_id":[UserHandle shareInstance].userId
                           };
    
    
    [BSDNetTool PostURL:kHost  parameters:arg succeed:^(NSDictionary *data) {
        NSLog(@"任务 = %@",data);
        
        if(self.page.pageIndex == 1){
            [self.page.arrayData removeAllObjects];
        }
        NSInteger flag = [[data objectForKey:@"status"]integerValue];
        if(flag == 1){
            self.page.recordCount = [[data objectForKey:@"total"] integerValue];
            self.page.pageSize = [[data objectForKey:@"number"]integerValue];
            NSInteger empty = self.page.recordCount % self.page.pageSize;
            if(empty == 0){
                self.page.pageCount = self.page.recordCount/self.page.pageSize;
            }else{
                self.page.pageCount = self.page.recordCount/self.page.pageSize+1;
            }
            
            NSMutableArray *tempArr = [OrderModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"list"]];
            [ self.page.arrayData addObjectsFromArray:tempArr];
            
            
            
            NSString *count = [NSString stringWithFormat:@"%@单",[data objectForKey:@"total"]];
            NSString* str = [NSString stringWithFormat:@"累计单量 : %@",count];
            NSMutableAttributedString* attributeStr = [[NSMutableAttributedString alloc]initWithString:str];
            [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[str rangeOfString:count]];
            self.titleLabel.attributedText = attributeStr;
            
            
            
        }else{
            [self alertHUD:[data objectForKey:@"fail"]];
        }
        
        [self.tableView reloadData];
        if(self.page.pageCount<=self.page.pageIndex){
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        if(self.page.pageIndex==1){
            [self.tableView.mj_header endRefreshing];
        }
        
        
        
    } failure:^(NSError *error) {
        [self alertHUD:@"网络异常"];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
    
}

-(void)refereshDatasource{
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf getData];
    }];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.page.arrayData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    OrderModel *model = self.page.arrayData[section];
    
    return model.orders.count;
    
}

//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//
//    return Cell_H;
//
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    

    FinishTaskTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FinishTaskTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    
    OrderModel *model = self.page.arrayData[indexPath.section];
    OrderModel *model1 =  model.orders[indexPath.row];
    cell.orderModel = model1;

    return cell;

    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    OrderModel *model = self.page.arrayData[indexPath.section];
//    OrderModel *model1 =  model.orders[indexPath.row];
//    
//    FinishTaskDetailViewController *VC = [[FinishTaskDetailViewController alloc] init];
//    VC.main_id = model1.main_id;
//    [self.navigationController pushViewController:VC animated:YES];

}



- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 10;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return 40;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor whiteColor];
    
    UILabel *title = [[UILabel alloc] init];
    title.textColor = [UIColor darkGrayColor];
    title.font = kFontNameSize(15);
    [headerView addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(headerView);
        make.left.equalTo(headerView).offset(10);
        make.width.equalTo(headerView).multipliedBy(0.5);
        
    }];
    
    
    UILabel *content = [[UILabel alloc] init];
    content.textColor = [UIColor darkGrayColor];
    content.textAlignment = NSTextAlignmentRight;
    content.font = kFontNameSize(15);
    [headerView addSubview:content];
    [content mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(headerView);
        make.left.equalTo(title.mas_right);
        make.right.equalTo(headerView).offset(-10);

    }];
    
    OrderModel *model = self.page.arrayData[section];

    title.text = model.date;
    content.text = [NSString stringWithFormat:@"完成%@单",model.count];

    return headerView;

}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return nil;
    
}


#pragma mark - property package

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorColor = [UIColor groupTableViewBackgroundColor];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
        if (@available(iOS 11.0, *))
        {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.estimatedRowHeight = 0;
            _tableView.estimatedSectionFooterHeight = 0;
            _tableView.estimatedSectionHeaderHeight = 0;
        }
        
        _tableView.estimatedRowHeight = 60;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        
        [_tableView registerClass:[FinishTaskTableViewCell class] forCellReuseIdentifier:@"FinishTaskTableViewCell"];
        
        
    }
    
    return _tableView;
    
    
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.backgroundColor = [UIColor whiteColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}



-(BSDPager *)page{
    if(!_page){
        _page = [[BSDPager alloc]init];
        _page.pageIndex = 1;
        
    }
    return _page;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
